﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using FY2013.Business;
using FY2013.Model;

namespace FY2013.WebUI
{
    // Note: For instructions on enabling IIS6 or IIS7 classic mode, 
    // visit http://go.microsoft.com/?LinkId=9394801

    public class MvcApplication : System.Web.HttpApplication
    {
        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute(
                "Default", // Route name
                "{controller}/{action}/{id}", // URL with parameters
                new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
            );

        }

        public void Session_Start(object sender, EventArgs e)
        {
            if (Session["UserSession"] == null || string.IsNullOrWhiteSpace(Session["UserSession"].ToString()))
            {
                return;
            }
            using(Entities entities=new Entities())
            {
                SessionHelper.AttachSession(Session,entities);
            }
        }

        public void Session_End(object sender, EventArgs e)
        {
            if (Session["UserSession"] == null || string.IsNullOrWhiteSpace(Session["UserSession"].ToString()))
            {
                return;
            }

            using (Entities entities = new Entities())
            {
                SessionHelper.DetachSession(Session,entities);
            }
        }

        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();

            RegisterRoutes(RouteTable.Routes);
        }
    }
}